/**
 * Created by Administrator on 2016/7/4.
 */
"use strict";
swan_pc
    .controller("TaskCalShowController",function($scope,$rootScope,$state,Task) {
        $scope.initCalendar=function(){
            var date = new Date();
            var d = date.getDate();
            var m = date.getMonth();
            var y = date.getFullYear();

            var calendar = $('#calendar').fullCalendar({
                header: {
                    left: 'prev,next today',
                    center: 'title',
                    right: 'month,agendaWeek'
                },
                defaultView:"agendaWeek",
                //selectable: true,
                selectHelper: true,
                select: function(start, end, allDay) {
                    var title = prompt('Event Title:');
                    if (title) {
                        calendar.fullCalendar('renderEvent',
                            {
                                title: title,
                                start: start,
                                end: end,
                                allDay: allDay
                            },
                            true // make the event "stick"
                        );
                    }
                    calendar.fullCalendar('unselect');
                },
                //editable: true,
                //editable:true,
                droppable: true, // this allows things to be dropped onto the calendar !!!
                drop: function(date, allDay) { // this function is called when something is dropped

                    // retrieve the dropped element's stored Event Object
                    var originalEventObject = $(this).data('eventObject');

                    // we need to copy it, so that multiple events don't have a reference to the same object
                    var copiedEventObject = $.extend({}, originalEventObject);

                    // assign it the date that was reported
                    copiedEventObject.start = date;
                    copiedEventObject.allDay = allDay;
                    // is the "remove after drop" checkbox checked?
                    if ($('#drop-remove').is(':checked')) {
                        // if so, remove the element from the "Draggable Events" list
                        $(this).remove();
                    }
                },
                eventClick:function(event,jsEvent,view){
                    $rootScope.presentTaskid=event.id;
                    $state.go("taskdetail");
                },
                dayClick:function(date, allDay, jsEvent, view){
                    $rootScope.clickedTaskDate=date;
                    $state.go("addtask");
                }
            })
        }
        /*$("#calendar").fullCalendar("dayClick",function(){
         alert("day click is right");
         });*/
        $scope.initCalendar();
        $scope.tasks = [];
        $scope.loadAll = function () {
            var login = $rootScope.login;
            Task.query(function(result){
                $scope.tasks=result;
                //console.log("result:"+result);
                //console.log("tasks:"+$scope.tasks);
                angular.forEach($scope.tasks,function(data,index,array){
                    var taskStatus=data.status.toString();
                    var startDate=new Date(data.startTime);
                    var endDate=new Date(data.endTime);
                    if(data.status=="CONFIRM") {
                        var events = [{
                            title: data.name,
                            start: startDate,
                            end: endDate,
                            id:data.id,
                            backgroundColor: Theme.colors.blue
                        }]
                        $("#calendar").fullCalendar("addEventSource", events);
                    }
                    if(data.status=="DRAFT"){
                        var events = [{
                            title: data.name,
                            start: startDate,
                            end: endDate,
                            id:data.id,
                            backgroundColor: Theme.colors.red
                        }]
                        $("#calendar").fullCalendar("addEventSource", events);
                    }
                    if(data.status!="DRAFT"&&data.status!="CONFIRM"){
                        var events = [{
                            title: data.name,
                            start: startDate,
                            end: endDate,
                            id:data.id,
                            backgroundColor: Theme.colors.gray
                        }]
                        $("#calendar").fullCalendar("addEventSource", events);
                    }
                });
                })
            }
        $scope.loadAll();
    });